#!/usr/bin/ruby
# -*- ruby -*-
#
# http://www.reddit.com/r/dailyprogrammer/comments/101m7y/9172012_challenge_99_easy_words_with_letters_in/

dictionary = ARGV.first || File.basename($0).gsub(/\.rb$/, '.input')

approach = :sorting

File.open(dictionary) do |dictionary_fh|
  dictionary_fh.readlines.each do |line|
    line = line.strip

    last_char = nil
    increasing = true

    if approach == :linear
      # Approach 1, O(n)
      line.split("").each do |ch|
        increasing = last_char.nil? || (last_char <= ch)

        break unless increasing

        last_char = ch
      end
    elsif approach == :sorting
      # Approach 2, O(n*logn)
      increasing = (line == line.chars.sort.join)
    else
      raise "Unknown approach '#{approach}'"
    end

    if increasing
      puts line
    end
  end
end
